package com.rongc.client.freight.utils.dic.impl;

import com.rongc.client.freight.utils.dic.IField;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class Table implements Serializable {

	private static final long serialVersionUID = -5762018049671827945L;
	// 表标识
	private long id;
	// 表名
	private String tableName;

	private String lable;

	// 表字段
	private HashMap<String, Field> fields = new HashMap<String, Field>();

	// private HashMap<Integer, ArrayList<Field>> fieldGroup = new
	// HashMap<Integer, ArrayList<Field>>();

	private ArrayList<Field> fieldList = new ArrayList<Field>();

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	/* public void addField(Field field) { // TODO 分组处理 ArrayList<Field>
	 * groupList = fieldGroup.get(field.getGroupId()); if (groupList == null) {
	 * groupList = new ArrayList<Field>(); fieldGroup.put(field.getGroupId(),
	 * groupList); } groupList.add(field);
	 * 
	 * // //////// fields.put(field.getFieldName(), field); } */

	public void addField(Field field) {
		if (fieldList == null) {
			fieldList = new ArrayList<Field>();
		}
		fields.put(field.getFieldName(), field);
		fieldList.add(field);
	}

	public Field getField(String fieldName) {

		return fields.get(fieldName);
	}


	/* public HashMap<Integer, ArrayList<Field>> getFieldGroup() {
	 * ComparatorUser comparator = new ComparatorUser(); for (ArrayList<Field>
	 * userlist : fieldGroup.values()) { Collections.sort(userlist, comparator);
	 * } return fieldGroup; } */

	public ArrayList<Field> getFieldList() {
		ComparatorUser comparator = new ComparatorUser();
		Collections.sort(fieldList, comparator);
		return fieldList;
	}

	class ComparatorUser implements Comparator {
		public int compare(Object user0, Object user1) {

			int flag = (((Field) user0).getOrder())
					- (((Field) user1).getOrder());
			if (flag == 0) {
				return ((IField) user0).getFieldName().compareTo(
						((IField) user1).getFieldName());
			} else {
				return flag;
			}
		}

	}
}
